package com.definesys.smecrailtransit;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.definesys.smecrailtransit.SmecManager.SmecRealm.RealmManager;
import com.definesys.smecrailtransit.SmecManager.SmecRetrofit.RetrofitServiceManager;
import com.definesys.smecrailtransit.SmecManager.SmecSession.SmecSession;
import com.definesys.smecrailtransit.service.LogService.AbnormalCollection;
import com.definesys.smecrailtransit.service.LogService.CrashHandler;
import com.kaopiz.kprogresshud.KProgressHUD;

import java.util.ArrayList;
import java.util.List;

import cn.jpush.android.api.JPushInterface;
import timber.log.Timber;

/**
 * Created by xupeizuo on 2017/7/20.
 *
 */

public class SmecApplication extends Application {

    private static Context mContext;
    private static SmecSession smecSession;
    private static PackageInfo mPackageInfo;
    private CrashHandler mCrashHandler;
    private static ArrayList<KProgressHUD> kProgressHUDs=new ArrayList<>();

    @Override
    public void onCreate() {
        super.onCreate();
        mContext=getApplicationContext();
        RealmManager.initRealm(mContext);
        smecSession=SmecSession.initSmecSession();
        mPackageInfo = _getPackageInfo(mContext);
        mCrashHandler=CrashHandler.getCrashHandler();
        mCrashHandler.init(mContext);
        RetrofitServiceManager.init();
        Timber.plant(new Timber.DebugTree());
        JPushInterface.setDebugMode(false);//正式版的时候设置false，关闭调试
        JPushInterface.init(this);
    }

    /**
     * 初始化包名信息
     * @param context
     * @return
     */
    private PackageInfo _getPackageInfo(Context context) {
        PackageInfo pi = null;

        try {
            PackageManager pm = context.getPackageManager();
            pi = pm.getPackageInfo(context.getPackageName(),
                    PackageManager.GET_CONFIGURATIONS);
            return pi;
        } catch (Exception e) {
            e.printStackTrace();
            AbnormalCollection.getAbnormalCollection().saveExceptionMsg(e, AbnormalCollection.EXCEPTION_TYPE_THROW_E);
        }
        return pi;
    }

    /**
     * 判断某个服务是否正在运行的方法
     *
     * @param mContext
     * @param serviceName
     *            是包名+服务的类名（例如：com.definesys.smecrailtransit.TestService）
     * @return true代表正在运行，false代表服务没有正在运行
     */
    public static boolean isServiceWork(Context mContext, String serviceName) {
        boolean isWork = false;
        ActivityManager myAM = (ActivityManager) mContext
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> myList = myAM.getRunningServices(40);
        if (myList.size() <= 0) {
            return false;
        }
        for (int i = 0; i < myList.size(); i++) {
            String mName = myList.get(i).service.getClassName().toString();
            if (mName.equals(serviceName)) {
                isWork = true;
                break;
            }
        }
        return isWork;
    }

    public static Context getContext(){
        return mContext;
    }

    public static SmecSession getSmecSession(){
        return smecSession;
    }

    public static PackageInfo getPackageInfo() {
        return mPackageInfo;
    }

    public static ArrayList<KProgressHUD> getkProgressHUDs() {
        return kProgressHUDs;
    }
}
